package com.android.inestpin;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.android.inestpin.cache.DiskLruCacheHelper;
import com.android.inestpin.model.group.HomeConfigData;
import com.android.inestpin.model.group.HomeConfigDataCallback;
import com.android.inestpin.model.group.MainSearchSortData;
import com.android.inestpin.model.group.MainSearchSortDataCallback;
import com.android.inestpin.utils.ConstantsUrl;
import com.android.inestpin.utils.HttpUtils;
import com.android.inestpin.utils.MyLog;
import com.android.inestpin.utils.ObjectUtils;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;

public class WelcomeActivity extends AppCompatActivity {
    public static WelcomeActivity intance = null;
    private WelcomeActivity mcontext;
    private DiskLruCacheHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(com.android.inestpin.R.layout.activity_welcome);
        intance = this;
        mcontext = WelcomeActivity.this;
        ObjectUtils.StatusBarWhiteBg(mcontext, false);
        try {
            helper = new DiskLruCacheHelper(mcontext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        GetSortData();
        getConfig();
        initData();
    }

    private void initData() {

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(mcontext, MainActivity.class));
                finish();

            }
        }, 1000);
    }

    private void GetSortData() {
        boolean networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.SORTLISTDATA;
            MyLog.e("地址", address);
            HttpUtils.get(address, null, new MainSearchSortDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(MainSearchSortData response, int id) {
                    if (response == null) {
                        return;
                    }
                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_SEARCH, response.toString());
                    }
                }
            });
        } else {

        }
    }

    private void getConfig() {
        boolean networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MAIN_CONFIG;
            HttpUtils.get(address, null, new HomeConfigDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                }

                @Override
                public void onResponse(HomeConfigData response, int id) {
                    if (response == null) {
                        return;
                    }
                    HomeConfigData.DataBean data = response.getData();
                    if (response.getCode() == 0 && data != null) {
                        String comm_limit_min = data.getComm_limit_min();
                        String comm_limit_max = data.getComm_limit_max();
                        SharedPreferences myPref = getSharedPreferences(ConstantsUrl.SHAREDPREFERENCES_NAME, 0);
                        SharedPreferences.Editor editor = myPref.edit();
                        editor.putString(ConstantsUrl.CACHE_HOME_LIMIT_MIN, comm_limit_min);
                        editor.putString(ConstantsUrl.CACHE_HOME_LIMIT_MAX, comm_limit_max);
                        editor.commit();
                    }
                }
            });
        }
    }
}
